package com.xuxueli.crawler.sample;

import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import com.xuxueli.crawler.parser.PageParser;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * TODO
 * @version 1.0
 * @author jiangyixin
 * @date 2020/12/21 下午4:38
 */
@PageSelect(cssQuery = "body")
public class BlogPage {
	
	@PageFieldSelect(cssQuery = "#mainScreen > div > div.blog-detail-container > div.detail-body-box > div > div.main-box > div > div.article-box.box-card.box-card--shadow > div > div.article-box__header > h1 > a")
	private String title;
	
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	@Override
	public String toString() {
		return "BlogPage{" +
				"title='" + title + '\'' +
				'}';
	}
	
	public static void main(String[] args) {
		XxlCrawler crawler = new XxlCrawler.Builder()
				.setUrls("https://my.oschina.net/xuxueli/blog/")
				.setAllowSpread(false)
				.setWhiteUrlRegexSet("https://my\\.oschina\\.net/xuxueli/blog/\\d+")
				.setThreadCount(1)
				.setPageParser(new PageParser<BlogPage>() {
					@Override
					public void parse(Document html, Element pageVoElement, BlogPage pageVo) {
						// 解析封装 PageVo 对象
						String pageUrl = html.baseUri();
						System.out.println(pageUrl + "：" + pageVo.toString());
					}
				})
				.build();
		crawler.start(true);
	}
}
